//index.js
const app = getApp();
var db = wx.cloud.database({
  env: "lemonbox-t-6wew9"
})
Page({
  data: {
    avatarUrl: './user-unlogin.png',
    userInfo: {},
    logged: false,
    takeSession: false,
    requestResult: '',
    thingList: [],
    defaultImg: "/images/default.png",
    toEndCount: 0,
    outDateCount: 0,
    nowDate:getDate(new Date()).getTime()
  },

  onLoad: function () {
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    })

    this.onGetOpenid()
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    


    db.collection('thing').where({
        state: 0,
      })
      .orderBy('bcheck', 'desc')
      .orderBy('endDate', 'asc')
      .get({
        success: function (res) {
          console.log(res)
          that.setData({
            thingList: res.data
          })
        }
      })

    wx.cloud.callFunction({
      name: 'getdatecount',
      complete: res => {
        wx.hideLoading({
          success: (res) => {},
        })
        console.log('callFunction test result: ', res)
        that.setData({
          toEndCount: res.result.toEndCount,
          outDateCount: res.result.outDateCount
        })
      },
      error: res => {
        wx.hideLoading({
          success: (res) => {
            wx.showToast({
              title: '查询失败',
            })
          },
        })
      }

    })

  },

  toDetail: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id,
    })
  },



  onGetUserInfo: function (e) {
    if (!this.data.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo
      })
    }
  },

  onGetOpenid: function () {
    // 调用云函数
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        console.log('[云函数] [login] user openid: ', res.result.openid)
        app.globalData.openid = res.result.openid
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
      }
    })
  },

  onAdd: function (e) {
    wx.navigateTo({
      url: '/pages/addthing/addthing',
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '物品收纳提醒实用工具',
      imageUrl: 'https://website-178c6e-1252504509.tcloudbaseapp.com/images/lemonbox/sharecover.png'
    }
  },
  onShareTimeline() {
    return {
      title: '物品收纳提醒实用工具'
    }
  }

})

function getDate(date) {
  var year = date.getFullYear()
  var month = date.getMonth()
  var day = date.getDate()
  var nDate = new Date(year, month, day)
  return nDate
}